package rest.xstream;

import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;
import entities.Channel;

/**
 * Class for channel correct displaying in XML
 * Channel id date
 * User: kaiser
 * Date: 8/2/13
 * Time: 10:23 PM
 *
 * @see //jira.codehaus.org/browse/XSTR-481
 */

/*
    If use implements Converter
    and implement methods marshal and unmarshal
    then faced with the problem:

    "If one is using the @XStreamConverter annotation on a field,
    XStream seems to ignore @XStreamAsAttribute altogether,
    and only reacts to an element, not an attribute."

    see: //jira.codehaus.org/browse/XSTR-481
*/

//public class ChannelIdConverter implements Converter {
public class ChannelIdConverter extends AbstractSingleValueConverter {

    @Override
    public boolean canConvert(Class aClass) {
        return aClass.equals(Channel.class);
    }

    @Override
    public Object fromString(String id) {
        Channel channel = new Channel();
//        /**
//         * @todo add correct enity
//         */
        channel.setId(id);
        return channel;
    }

    @Override
    public String toString(Object c) {
        Channel channel = (Channel) c;
        String id = channel.getId();
        if (id != null)
            return id;

        return null;
    }
            /* Old variant */
//    @Override
//    public void marshal(Object o, HierarchicalStreamWriter hierarchicalStreamWriter, MarshallingContext marshallingContext) {
//        Channel channel = (Channel) o;
//        hierarchicalStreamWriter.setValue(channel.getId());
//    }
//
//    @Override
//    public Object unmarshal(HierarchicalStreamReader hierarchicalStreamReader, UnmarshallingContext unmarshallingContext) {
//        Channel channel = new Channel();
//        channel.setId(hierarchicalStreamReader.getValue());
//        return channel;
//    }
}
